package middle;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import middle.Solution_71.Status;

public class Solution_385 {

    public static void main(String[] args) {
        Solution model = new Solution();
        NestedInteger rst = model.deserialize("324");
//        NestedInteger deserialize = model.deserialize("[123,[456,[789]]]");
        NestedInteger deserialize = model.deserialize("[123,456,[788,799,833],[[]],10,[]]");
        // "[123,456,[788,799,833],[[]],10,[]]"
        System.out.println();
    }

    static class Solution {
        public NestedInteger deserialize(String s) {
            s = "[" + s + "]";
            char[] chars = s.toCharArray();
            NestedInteger ni = null;
            Stack<NestedInteger> stack = new Stack<>();
            StringBuilder builder = new StringBuilder();
            for (char c : chars) {
                if (c == '[') {
                    if (ni != null) {
                        stack.add(ni);
                    }
                    ni = new NestedInteger();
                } else if (c == ']') {
                    if (builder.length() > 0) {
                        int val = Integer.parseInt(builder.toString());
                        ni.add(new NestedInteger(val));
                        builder.delete(0, builder.length());
                    }

                    if (stack.empty()) {
                        break;
                    }
                    stack.peek().add(ni);
                    ni = stack.pop();
                } else if (c == ',') {
                    if (builder.length() > 0) {
                        int val = Integer.parseInt(builder.toString());
                        ni.add(new NestedInteger(val));
                        builder.delete(0, builder.length());
                    }
                } else {
                    builder.append(c);
                }
            }
            return ni.getList().get(0);
        }
    }

      public static class NestedInteger {

        private int val;
        private List<NestedInteger> list;
        private boolean isInteger;
          // Constructor initializes an empty nested list.
          public NestedInteger() {
              this.isInteger = false;
              list = new ArrayList<>();
          }

          // Constructor initializes a single integer.
          public NestedInteger(int value) {
              this.val = value;
              this.isInteger = true;
          }

          // @return true if this NestedInteger holds a single integer, rather than a nested list.
          public boolean isInteger() {
              return isInteger;
          }

          // @return the single integer that this NestedInteger holds, if it holds a single integer
          // Return null if this NestedInteger holds a nested list
          public Integer getInteger() {
              return val;
          }

          // Set this NestedInteger to hold a single integer.
          public void setInteger(int value) {
              this.val = value;
          }

          // Set this NestedInteger to hold a nested list and adds a nested integer to it.
          public void add(NestedInteger ni) {
              list.add(ni);
          }

          // @return the nested list that this NestedInteger holds, if it holds a nested list
          // Return empty list if this NestedInteger holds a single integer
          public List<NestedInteger> getList() {
              return list;
          }
      }
}
